package pw.archipelag.loaderGame.panel {
	import flash.display.Sprite;
	import flash.display.DisplayObject;
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinBg;
	import flash.utils.getQualifiedClassName;
	import flash.utils.getQualifiedSuperclassName;
	import flash.display.Bitmap;
	import flash.display.Shape;
	import flash.utils.getDefinitionByName;
	import flash.geom.Rectangle;
	import base.dspObj.ScaleObject;
	import com.greensock.TweenMax;
	
	public class Bg extends Sprite {
		
		[Embed(source="../!embed/skin/bgPanelMiniDefault.png")]
		public static const classBmpDataBgPanelMiniDefault: Class;
		
		[Embed(source="../!embed/skin/bgPanelStartEndGameDefault.png")]
		public static const classBmpDataBgPanelStartEndGameDefault: Class;
		
		[Embed(source="../!embed/skin/bgBtnPanelDefault.png")]
		public static const classBmpDataBgBtnPanelDefault: Class;
		
		private var isPanelMiniStartEndGameBtnPanel: uint;
		private var shape: DisplayObject;
		
		public function Bg(isPanelMiniStartEndGameBtnPanel: uint): void {
			this.isPanelMiniStartEndGameBtnPanel = isPanelMiniStartEndGameBtnPanel;
			this.draw();
		}
		
		public function clear(): void {
			this.graphics.clear();
			if (this.shape)	this.removeChild(this.shape);
			this.shape = null;
		}
		
		public function draw(skin: SkinBg = null): void {
			skin = skin || new SkinBg();
			var currWidth: Number = this.width;
			var currHeight: Number = this.height;
			this.clear();
			this.scaleX = this.scaleY = 1;
			var isSkinShapeClassBitmapSpriteShape: int = -1;
			if (skin.shape) {
				if (skin.shape is Class)
					isSkinShapeClassBitmapSpriteShape = 0
				if ((getQualifiedClassName(skin.shape).indexOf("Bitmap") != -1) || (getQualifiedSuperclassName(skin.shape).indexOf("Bitmap") != -1)) 
					isSkinShapeClassBitmapSpriteShape = 1;
				else if ((getQualifiedClassName(skin.shape).indexOf("Sprite") != -1) || (getQualifiedSuperclassName(skin.shape).indexOf("Sprite") != -1)) 
					isSkinShapeClassBitmapSpriteShape = 2;
				else if ((getQualifiedClassName(skin.shape).indexOf("Shape") != -1) || (getQualifiedSuperclassName(skin.shape).indexOf("Shape") != -1)) 
					isSkinShapeClassBitmapSpriteShape = 3;
			}
			if (isSkinShapeClassBitmapSpriteShape > -1) {
				var classSkinShape: Class;
				if (isSkinShapeClassBitmapSpriteShape == 0) classSkinShape = skin.shape;
				else {
					if (isSkinShapeClassBitmapSpriteShape != 1) this.graphics.copyFrom(skin.shape.graphics);
					if (isSkinShapeClassBitmapSpriteShape != 3) classSkinShape = Class(getDefinitionByName(getQualifiedClassName(skin.shape)));
				}
				if (classSkinShape) this.shape = new classSkinShape();
			} else {
				this.shape = new [Bg.classBmpDataBgPanelMiniDefault, Bg.classBmpDataBgPanelStartEndGameDefault, Bg.classBmpDataBgBtnPanelDefault][this.isPanelMiniStartEndGameBtnPanel]();
				if (this.isPanelMiniStartEndGameBtnPanel == 0) skin.radiusOrRectForScaling = new Rectangle(40, 40, 10, 10);
				else if (this.isPanelMiniStartEndGameBtnPanel == 1) skin.radiusOrRectForScaling = new Rectangle(120, 120, 460, 540);
				else if (this.isPanelMiniStartEndGameBtnPanel == 2) skin.radiusOrRectForScaling = new Rectangle(30, 10, 30, 10);
			}
			var scale9Grid: Rectangle;
			if (skin.radiusOrRectForScaling is Rectangle) scale9Grid = skin.radiusOrRectForScaling;
			else scale9Grid = new Rectangle(skin.radiusOrRectForScaling, skin.radiusOrRectForScaling, this.width - 2 * skin.radiusOrRectForScaling, this.height - 2 * skin.radiusOrRectForScaling);
			if ((this.shape) && (this.shape.width) && (this.shape.height)) {
				this.shape = new ScaleObject(this.shape, scale9Grid);
				this.addChild(this.shape);
			} else this.scale9Grid = scale9Grid;
			
			this.alpha = skin.alpha;
			this.width = currWidth || this.width;
			this.height = currHeight || this.height;
			TweenMax.to(this, 0, skin.objTween);
		}

		override public function get width(): Number {
			var width: Number;
			if (this.shape) width = this.shape.width;
			else width = super.width;
			return width;
		}
		
		override public function get height(): Number {
			var height: Number;
			if (this.shape) height = this.shape.height;
			else height = super.height;
			return height;
		}
		
		override public function set width(value: Number): void {
			if (this.shape) this.shape.width = Math.round(value);
			else super.width = value;
		}
		
		override public function set height(value: Number): void {
			if (this.shape) this.shape.height = Math.round(value);
			else super.height = value;
		}
		
	}

}